﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-06-15 08:45:45 +0200 (Mon, 15 Jun 2009) $
// Last Changed Revision $Rev: 194 $
//
// ====================================================================================================
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Tum.CollabXT.Wizard
{
    /// <summary>
    /// Interaction logic for SelectProcessProviderPage.xaml
    /// </summary>
    internal partial class SelectProcessProviderPage : InternalConversionWorkflowPage
    {
        /// <summary>
        /// Conversion workflow to be used.
        /// </summary>
        ConversionWorkflow Workflow;


        /// <summary>
        /// Constructor.
        /// </summary>
        public SelectProcessProviderPage()
        {
            InitializeComponent();
            
            Helper.FillInlineCollectionFromString(textBlockSelectProcessProvider.Inlines, Wizard.Resources.Language.ProcessProviderPage_SelectProcessProvider);
        }

        /// <summary>
        /// Receives the conversion workflow.
        /// </summary>
        /// <param name="workflow">Conversion workflow.</param>
        /// <returns>True, if the workflow has been received correctly, false if not.</returns>
        public override bool InternalReceiveWorkflowHandle(ConversionWorkflow workflow)
        {
            Workflow = workflow;

            PopulateProviderList();
            return true;
        }

        /// <summary>
        /// Next page has been requested.
        /// </summary>
        /// <returns>True, if the page change is allowed (process provider has been selected), false if not.</returns>
        public override bool NextPageRequested()
        {
            if (Workflow == null)
                return true;

            return Workflow.Project.ProcessProvider != null;
        }

        /// <summary>
        /// Populates the process provider list.
        /// </summary>
        private void PopulateProviderList()
        {
            Workflow.UpdateButtonState(true, false);

            foreach (ProcessProviderInfo curProviderInfo in Workflow.PluginMgr.GetProcessProviders())
            {
                //Create provider radio button
                RadioButton providerButton = new RadioButton();
                providerButton.Content = curProviderInfo.Name;
                providerButton.FontWeight = FontWeight.FromOpenTypeWeight(700);
                providerButton.Tag = curProviderInfo;
                providerButton.Checked += new RoutedEventHandler(ProviderRadioButtonCheckedHandler);
                providerButton.IsChecked = curProviderInfo.Equals(Workflow.Project.ProcessProvider);

                //Create description text
                TextBlock descriptionTextBlock = new TextBlock();
                descriptionTextBlock.Text = curProviderInfo.Description;
                descriptionTextBlock.Margin = new Thickness(15, 0, 0, 0);
                descriptionTextBlock.TextWrapping = TextWrapping.Wrap;
                descriptionTextBlock.Tag = providerButton;
                descriptionTextBlock.MouseLeftButtonDown += new MouseButtonEventHandler(descriptionTextBlock_MouseLeftButtonDown);

                //Add both
                stackPanelProviders.Children.Add(providerButton);
                stackPanelProviders.Children.Add(descriptionTextBlock);
            }
        }

        /// <summary>
        /// Event handler: Description mouse down.
        /// </summary>
        void descriptionTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Text has been clicked - select the belonging provider
            TextBlock senderBlock = sender as TextBlock;
            if (senderBlock.Tag is RadioButton)
                ((RadioButton)senderBlock.Tag).IsChecked = true;
        }

        /// <summary>
        /// Event handler: Provider radio button checked.
        /// </summary>
        public void ProviderRadioButtonCheckedHandler(Object sender, RoutedEventArgs e)
        {
            var newProvider = ((RadioButton)sender).Tag as ProcessProviderInfo;
            if (newProvider != Workflow.Project.ProcessProvider)
            {
                Workflow.Project.ProcessProvider = newProvider;
                Workflow.InvalidateProcessing();
            }

            //Enable next
            Workflow.UpdateButtonState(true, true);
        }
    }
}
